package com.ccit.dao;

import com.ccit.domain.Approval;
import com.ccit.domain.Punch;
import org.apache.ibatis.annotations.*;
import java.util.List;


@Mapper
public interface ApprovalDao {

    @Insert("insert into Approval (id,employeeName, employeeId, leaveType, startDate, endDate) values(#{id}, #{employeeName}, #{employeeId}, #{leaveType}, #{startDate}, #{endDate})")
    public int add(Approval approval);

    @Insert("insert into Punch (employeeName, employeeId, startDate, endDate) values( #{employeeName}, #{employeeId},  #{startDate}, #{endDate})")
    public int addPunch(Punch punch);

    //查询所有员工信息
    @Select("select * from Approval")
    public List<Approval> selectALl();

    //根据ID查询
    @Select("SELECT * FROM approval WHERE employeeId = #{employeeId} ORDER BY startDate desc")
    public List<Approval> getById(Integer id);

    //修改
    @Update("UPDATE Approval SET employeeId = #{employeeId}, leaveType = #{leaveType}, startDate = #{startDate}, endDate = #{endDate}, status = #{status},employeeName = #{employeeName} WHERE id = #{id}")
    public int update(Approval approval);

    @Update("UPDATE Punch SET employeeId = #{employeeId}, startDate = #{startDate}, endDate = #{endDate}, status = #{status},employeeName = #{employeeName} WHERE id = #{id}")
    public int updatePunch (Punch punch);
    //将 "attendance" 表中的签到时间（checkIn）和签退时间（checkOut）更新为与 "punch" 表中相匹配的对应值。
    // 通过内连接（INNER JOIN）两个表并设置更新的列和值，并通过日期匹配条件来确保更新仅在日期相等时进行。
    @Update("UPDATE attendance \n" +
            "JOIN punch ON attendance.employeeId = punch.employeeId\n" +
            "SET attendance.checkIn = punch.startDate,\n" +
            "    attendance.checkOut = punch.endDate\n" +
            "WHERE DATE(punch.startDate) = attendance.checkDate;")
    public int updateAttendance(Punch punch);

    //根据ID删除
    @Delete("delete from Approval where employeeId = #{employeeId}")
    public int delete(Integer id);


    /**
     * 分页查询
     * @param begin
     * @param size
     * @return
     */
    @Select("select * from approval ORDER BY startDate DESC limit #{begin} , #{size}")
    List<Approval> selectByPage(@Param("begin") int begin, @Param("size") int size);

    @Select("select * from punch ORDER BY startDate DESC limit #{begin} , #{size}")
    List<Punch> selectByPunchPage(@Param("begin") int begin, @Param("size") int size);

    //根据ID查询
    @Select("SELECT * FROM punch WHERE employeeId = #{employeeId} ORDER BY startDate desc")
    public List<Punch> getPunchById(Integer id);



    /**
     * 查询总记录数
     * @return
     */
    @Select("select count(*) from approval ")
    int selectTotalCount();

    @Select("select count(*) from punch ")
    int selectPunchTotalCount();


    @Select("SELECT COUNT(DISTINCT EmployeeId) AS LeaveCount " +
            "FROM Approval " +
            "WHERE StartDate >= #{startDate} AND EndDate <= #{endDate}")
    List<Approval> selectByDate(@Param("startDate") String startDate, @Param("endDate") String endDate);








}
